<?php 
namespace Modules\ShopAdmin\Http\Controllers\v1;
use Illuminate\Http\Request;
use Modules\AuthAdmin\Http\Requests\CommonIdRequest;
use Modules\AuthAdmin\Http\Requests\CommonStatusRequest;
use Modules\AuthAdmin\Http\Requests\CommonSortRequest;
use Modules\AuthAdmin\Services\CommonService;
use Modules\ShopAdmin\Models\ShopProductType as ShopProductTypeModel;
class ProductTypeController extends CommonController
{
	/**
	 * @name  商品分类
	 * @method  GET	
	 * @return  JSON
	 */ 
    public function index(Request $request)
    {
		return (new ShopProductTypeModel())->product_type_data();
    }
	/**
	 * @name  添加
	 * @method  POST	
	 * @param  pid int 父级ID	
	 * @param  level int 级别	
	 * @param  title string 分类名称	
	 * @param  image_id int 图片id
	 * @param  sort int 排序	
	 * @return  JSON
	 */
    public function store(Request $request)
    {
		$data = $request->all();
		return (new ShopProductTypeModel())->common_create([
			'pid'=>$data['pid'],
			'level'=>$data['level'],
			'title'=>$data['title'],
			'image_id'=>$data['image_id'],
			'sort'=>$data['sort'],
		]);
    }
	/**
	 * @name  添加子级
	 * @method  POST	
	 * @param  pid int 父级ID	
	 * @param  level int 级别	
	 * @param  title string 分类名称	
	 * @param  image_id int 图片id
	 * @param  sort int 排序	
	 * @return  JSON
	 */
    public function create_sublevel(Request $request)
    {
		$data = $request->all();
		return (new ShopProductTypeModel())->common_create([
			'pid'=>$data['pid'],
			'level'=>$data['level'],
			'title'=>$data['title'],
			'image_id'=>$data['image_id'],
			'sort'=>$data['sort'],
		]);
    }
	/**
	 * @name  编辑页面
	 * @method  GET	
	 * @param  id int 分类id	
	 * @return  JSON
	 */
    public function edit(CommonIdRequest $request)
    {	
		$ShopProductTypeModel = new ShopProductTypeModel();
		$data = $ShopProductTypeModel->select('id','title','image_id','sort')->with([
					'image_one'=>function($query){
						$query->select('id','url','open');  
					}
				])->find($request->get('id'));
		$CommonService = new CommonService();
		if(isset($data['image_one']) && $data['image_one']){
			if($data['image_one']['open'] == 1){
				$data['image_url'] = $CommonService->get_http_type().$data['image_one']['url'];
			}else{
				$data['image_url'] = $data['image_one']['url'];
			}
		}
		unset($data['image_one']);
		return $ShopProductTypeModel->api_success([
			'data'=>$data
		]);
    }
	/**
	 * @name  编辑提交
	 * @method  PUT	
	 * @param id int 分类id
	 * @param  title string 分类名称	
	 * @param  image_id int 图片id
	 * @param  sort int 排序
	 * @return  JSON
	 */ 
    public function update(Request $request)
    {
		$data = $request->all();
		return (new ShopProductTypeModel())->common_update($data['id'],[
			'title'=>$data['title'],
			'image_id'=>$data['image_id'],
			'sort'=>$data['sort'],
		]); 
    }
	/**
	 * @name  状态
	 * @method  PUT	
	 * @param  id int 分类id
	 * @param  status int 状态（0或1）
	 * @return  JSON
	 */ 
    public function status(CommonStatusRequest $request)
    {
		$data = $request->all();
		return (new ShopProductTypeModel())->common_status_update($data['id'],['status'=>$data['status']]); 
    }
	/**
	 * @name  排序
	 * @method  PUT	
	 * @param  id int 分类id
	 * @param  sort int 排序
	 * @return  JSON
	 */ 
    public function sorts(CommonSortRequest $request)
    {
		$data = $request->all();
		return (new ShopProductTypeModel())->common_sort_update($data['id'],['sort'=>$data['sort']]); 
    }
}
